#include "roleman.h"
#include "cgamemap.h"
#include "monster.h"
#include <QRect>
#include <QPixmap>
#include <QPainter>
#include <QFont>
#include <qnamespace.h>
#include<QEvent>
#include<QKeyEvent>
#include<QMessageBox>
#include <QDebug>
#include <QObject>
#include<QPaintEvent>
#include<QPainter>
#include <QPushButton>
#include <QMainWindow>

void Roleman::HeroInit(){//玩家初始化
    m_Lv = 1;
    m_Hp= m_MaxHp =1000;
    m_Att = 100;
    m_Def = 100;
    m_Exp =0;
    m_Money=0;
    m_BlueKey = 1;
    m_RedKey = 1;
    m_YellowKey = 1;
    floor=0;
    m_PosX=13;
    m_PosY=8;
}


/*0地面
 * 1墙
 * 2公主
 * 3岩浆
 * 4上楼梯
 * 5下楼梯
 * 6铁剑
 * 9玩家
 * 14-19红蓝黄钥匙门
 * 24-28 hp/经验药水 02宝石、52宝石、14宝石
 * >=40都是怪物
 */

void Roleman::ChangeFloor(bool a)//0下层，1上层
            {if(a==false){
                floor++;}
             else {floor--;}
            }

void Roleman::FlyFloor(int s){
                floor=s;

            }



void Roleman::HeroMove(CGameMap &gamemap,QKeyEvent *event) {//玩家移动
                //获取输入
                int OffSetX = 0, OffSetY = 0;
                switch (event->key())
                {
                case Qt::Key_Up:
                    OffSetX = -1;
                    break;
                case Qt::Key_Down:
                    OffSetX = 1;
                    break;
                case Qt::Key_Right:
                    OffSetY = 1;
                    break;
                case Qt::Key_Left:
                    OffSetY = -1;
                    break;
                default:
                    break;
                }
                if (0 == gamemap.m_Map[GetFloor()][m_PosX + OffSetX][m_PosY+OffSetY])//地面
                {
                    //修改地图的数据
                    gamemap.m_Map[GetFloor()][m_PosX + OffSetX][m_PosY + OffSetY] = 9;
                    gamemap.m_Map[GetFloor()][m_PosX][m_PosY] = 0;
                    //修改玩家的坐标
                    m_PosX += OffSetX;
                    m_PosY += OffSetY;
                }
            else if (1 == gamemap.m_Map[GetFloor()][m_PosX + OffSetX][m_PosY+OffSetY]||
                3== gamemap.m_Map[GetFloor()][m_PosX + OffSetX][m_PosY+OffSetY])//墙和岩浆
            {
            }
            else if (
                4== gamemap.m_Map[GetFloor()][m_PosX + OffSetX][m_PosY+OffSetY])//上楼
            {       ChangeFloor(1);
            }
            else if (
                5== gamemap.m_Map[GetFloor()][m_PosX + OffSetX][m_PosY+OffSetY])//上楼
            {       ChangeFloor(0);
            }
                else if (6 == gamemap.m_Map[GetFloor()][m_PosX + OffSetX][m_PosY + OffSetY])//铁剑
            {
                gamemap.m_Map[GetFloor()][m_PosX + OffSetX][m_PosY + OffSetY] = 9;
                gamemap.m_Map[GetFloor()][m_PosX][m_PosY] = 0;
                m_PosX += OffSetX;
                m_PosY += OffSetY;
                     if(HaveSword()==0){
                     m_Sword=10;
                     m_Att+=m_Sword;}
                     else{m_Att=m_Att+10-m_Sword;m_Sword=10;}}

                else if (14 == gamemap.m_Map[GetFloor()][m_PosX + OffSetX][m_PosY + OffSetY])//红钥匙
                {
                    gamemap.m_Map[GetFloor()][m_PosX + OffSetX][m_PosY + OffSetY] = 9;
                    gamemap.m_Map[GetFloor()][m_PosX][m_PosY] = 0;
                    //修改玩家的坐标
                    m_PosX += OffSetX;
                    m_PosY += OffSetY;
                    m_RedKey += 1;
                }
                else if (15 == gamemap.m_Map[GetFloor()][m_PosX + OffSetX][m_PosY + OffSetY])//蓝钥匙
                {
                    gamemap.m_Map[GetFloor()][m_PosX + OffSetX][m_PosY + OffSetY] = 9;
                    gamemap.m_Map[GetFloor()][m_PosX][m_PosY] = 0;
                    //修改玩家的坐标
                    m_PosX += OffSetX;
                    m_PosY += OffSetY;
                    m_BlueKey += 1;
                    /*char *m=intToWchar(m_BlueKey);
                    outtextxy(32 * 18 + 32 * 3, 32 * 2, *m);*/
                }
                else if (16 == gamemap.m_Map[GetFloor()][m_PosX + OffSetX][m_PosY + OffSetY])//黄钥匙
                {
                    gamemap.m_Map[GetFloor()][m_PosX + OffSetX][m_PosY + OffSetY] = 9;
                    gamemap.m_Map[GetFloor()][m_PosX][m_PosY] = 0;
                    //修改玩家的坐标
                    m_PosX += OffSetX;
                    m_PosY += OffSetY;
                    m_YellowKey += 1;
                }
                else if (17 == gamemap.m_Map[GetFloor()][m_PosX + OffSetX][m_PosY + OffSetY])//红门
            {
                if (0 != m_RedKey)
                {
                gamemap.m_Map[GetFloor()][m_PosX + OffSetX][m_PosY + OffSetY] = 9;
                gamemap.m_Map[GetFloor()][m_PosX][m_PosY] = 0;
                //修改玩家的坐标
                m_PosX += OffSetX;
                m_PosY += OffSetY;
                m_RedKey -= 1;
                }

            }
                else if (18 == gamemap.m_Map[GetFloor()][m_PosX + OffSetX][m_PosY + OffSetY])//蓝门
            {
                if (0 != m_BlueKey)
                {
                    gamemap.m_Map[GetFloor()][m_PosX + OffSetX][m_PosY + OffSetY] = 9;
                    gamemap.m_Map[GetFloor()][m_PosX][m_PosY] = 0;
                    //修改玩家的坐标
                    m_PosX += OffSetX;
                    m_PosY += OffSetY;
                    m_BlueKey -= 1;
                }

            }
                else if (19 == gamemap.m_Map[GetFloor()][m_PosX + OffSetX][m_PosY + OffSetY])//黄门
            {
                if (0 != m_YellowKey)
                {
                    gamemap.m_Map[GetFloor()][m_PosX + OffSetX][m_PosY + OffSetY] = 9;
                    gamemap.m_Map[GetFloor()][m_PosX][m_PosY] = 0;
                    //修改玩家的坐标
                    m_PosX += OffSetX;
                    m_PosY += OffSetY;
                    m_YellowKey -= 1;
                }

            }
                else if (24 == gamemap.m_Map[GetFloor()][m_PosX + OffSetX][m_PosY + OffSetY])//hp药水
                {
                        m_Hp += 100;

                    gamemap.m_Map[GetFloor()][m_PosX + OffSetX][m_PosY + OffSetY] = 9;
                    gamemap.m_Map[GetFloor()][m_PosX][m_PosY] = 0;
                    m_PosX += OffSetX;
                    m_PosY += OffSetY;

                }
                else if (25 == gamemap.m_Map[GetFloor()][m_PosX + OffSetX][m_PosY + OffSetY])//经验药水
                {
                    gamemap.m_Map[GetFloor()][m_PosX + OffSetX][m_PosY + OffSetY] = 9;
                    gamemap.m_Map[GetFloor()][m_PosX][m_PosY] = 0;
                    m_PosX += OffSetX;
                    m_PosY += OffSetY;
                    if ((m_Exp + 20) >= 100)
                    {
                        m_Lv += 1;
                        m_Exp = 100-m_Exp+20;
                    }
                    else
                    {
                        m_Exp += 20;
                    }

                }
                else if (26 == gamemap.m_Map[GetFloor()][m_PosX + OffSetX][m_PosY + OffSetY])//防御宝石
                {
                    gamemap.m_Map[GetFloor()][m_PosX + OffSetX][m_PosY + OffSetY] = 9;
                    gamemap.m_Map[GetFloor()][m_PosX][m_PosY] = 0;
                    m_PosX += OffSetX;
                    m_PosY += OffSetY;
                    m_Def += 2;
                }
                else if (27 == gamemap.m_Map[GetFloor()][m_PosX + OffSetX][m_PosY + OffSetY])//红宝石
                {
                    gamemap.m_Map[GetFloor()][m_PosX + OffSetX][m_PosY + OffSetY] = 9;
                    gamemap.m_Map[GetFloor()][m_PosX][m_PosY] = 0;
                    m_PosX += OffSetX;
                    m_PosY += OffSetY;
                    m_Att += 5;
                    m_Def += 2;
                }
                else if (28 == gamemap.m_Map[GetFloor()][m_PosX + OffSetX][m_PosY + OffSetY])//蓝宝石
                {
                    gamemap.m_Map[GetFloor()][m_PosX + OffSetX][m_PosY + OffSetY] = 9;
                    gamemap.m_Map[GetFloor()][m_PosX][m_PosY] = 0;
                    m_PosX += OffSetX;
                    m_PosY += OffSetY;
                    m_Att += 1;
                    m_Def += 4;
                }

                else if (39<gamemap.m_Map[GetFloor()][m_PosX + OffSetX][m_PosY + OffSetY])//怪物
            {
                int num=gamemap.m_Map[GetFloor()][m_PosX + OffSetX][m_PosY + OffSetY];
                    if(HeroFight(num)) // 实现战斗的逻辑
                    {gamemap.m_Map[GetFloor()][m_PosX + OffSetX][m_PosY + OffSetY] = 9;
                    gamemap.m_Map[GetFloor()][m_PosX][m_PosY] = 0;
                    //修改玩家的坐标
                    m_PosX += OffSetX;
                    m_PosY += OffSetY;
                    }
                    else{gamemap.m_Map[GetFloor()][m_PosX][m_PosY] = 0;}

            }
            else if (2 == gamemap.m_Map[GetFloor()][m_PosX + OffSetX][m_PosY + OffSetY])//公主//胜利
        {
                QMessageBox winmessage(QMessageBox::NoIcon,"You win!","You saved the princess!");
                winmessage.exec();
        }
  }
bool Roleman::HeroFight(const int &id){//战斗逻辑
        monster a(id);
        if(a.GetAtt()<=m_Def)
            {m_Money+=a.GetMoney();m_Exp+=a.GetExp();}
        else if(a.GetDef()>=m_Att)
        {
        QMessageBox diemessage(QMessageBox::NoIcon,"You died!","The Monster is too strong...You cannot defeated it!");
        diemessage.exec();
          return false;}
            else {
                int loseHp=a.GetHp()/(m_Att-a.GetDef())*(a.GetAtt()-m_Def);
                if(loseHp>=m_Hp){
                    QMessageBox diemessage(QMessageBox::NoIcon,"You died!","The Monster is too strong...You cannot defeated it!");
                    diemessage.exec();
                    return false;
                }
                else{
                    m_Hp-=loseHp;
                    m_Money+=a.GetMoney();m_Exp+=a.GetExp();
                    return true;}
        }
        return true;
            }


